<template>
	<view class="container">
		<view v-for="(item, index) in list" :key="index" class="feed-item" @click="itemClick(item, index)">
			<view class="title">{{item.childName}} 喂养记录</view>
			<view class="time" v-if="item.date">{{$u.timeFormat(item.date, 'yyyy年mm月dd日 hh:MM:ss')}}</view>
			<view class="state" :class="item.processed ? 'state-read':'state-unread'">{{item.processed === true ? '已读' : '未读'}}</view>
		</view>
		<view style="margin-top: 50rpx;">
			<u-loadmore v-if="!emptyShow" :status="status" />
		</view>
		<u-empty class="noContent" v-if="emptyShow" text="暂时没有宝宝的动态哦～"  mode="list"></u-empty>
		<fab v-if="appType==='app-p' || appType === 'mp-p'" @fabClick="fabClick"/>
		
		<u-popup v-model="show" mode="bottom">
			<view style="padding: 30rpx;">
				<!-- <view>喂养叮嘱:</view> -->
				<view>{{content}}</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import { getRecordFeed, getRecordFeedByTch, updateRecordFeed } from '@/api/record.js'
	import pageMixin from '@/mixins/page.js'
	import Fab from '@/components/fab.vue'
	export default {
		mixins: [pageMixin],
		components: {
			Fab
		},
		data() {
			return {
				show: false,
				content: ''
			}
		},
		computed: {
			...mapState({
				appType: state => state.app.appType,
				parentInfo: state => state.user.parentInfo,
			}),
		},
		onLoad() {
			this.getList(this.page);
		},
		onShow() {
			this.refresh()
		},
		methods: {
			refresh() {
				this.page = -1;
				this.getList(this.page);
			},
			getList(page) {
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					getRecordFeedByTch({pageNum: page + 1, pageSize: 20}).then(res => {
						console.log(res)
						this.setData(res);
					})
				} else {
					const childrenId = this.parentInfo.defaultBabyId;
					getRecordFeed({pageNum: page + 1, pageSize: 20, childrenId: childrenId}).then(res => {
						console.log(res)
						this.setData(res);
					})
				}

			},
			
			itemClick(item, index) {
				this.content = item.content
				this.show = true
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					updateRecordFeed({processed: true, id: item.id}).then(res => {
						console.log(res)
						item.processed = true
						this.list.splice(index, 1, item);
					})
				} 
			},
			
			fabClick() {
				this.$u.route({
					url: '/pages_sub/record/feed-page',
					params: {}
				})
			}
		}
	}
</script>

<style lang="scss">
	.container{
		display: flex;
		flex-direction: column;
		padding-left: 30rpx;
		padding-right: 30rpx;
	}
	.feed-item{
		border: 2rpx solid rgba(40,168,113,0.15);
		box-shadow: 0px 8rpx 0px 0px rgba(40,168,113,0.15);
		border-radius: 12rpx;
		
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 30rpx;
		margin-top: 30rpx;
		color: #999999;
		
		.title{
			flex: 1;
			font-weight: 400;
			color: #999999;
		}
		
		.time{
			margin-right: 10rpx;
		}
		
		.state-read{
			color: #28A871;
		}
		
		.state-unread{
			color: #f3a235;
		}
	}
</style>
